// pages/details/details.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        foodName:null,
        foodDetails:[],
        num:0,
        comment:[]
    },
      // 点击评论
      liickComment(e){
        wx.navigateTo({
          url:  `/pages/commentDetails/commentDetails?id=${this.data.comment[e.currentTarget.id]._id}&userTelphone=${this.data.comment[e.currentTarget.id].userTelphone}&headImage=${this.data.comment[e.currentTarget.id].headImage}&username=${this.data.comment[e.currentTarget.id].username}`
        })
      },
      // 获取评价列表
      getCommentList(){
        const db = wx.cloud.database()
        db.collection('CR_comments').where({
          belong : this.data.foodDetails[0]._id
        }).get({
          success:(res)=>{
            this.setData({
              comment:res.data
            })
            setTimeout(()=>{
              this.getuserInfo()
            },500)
          }
        })

      },
      //获取用户信息
      getuserInfo(){
        let user = this.data.comment
        let db = wx.cloud.database()
        let that = this
        for(let i in this.data.comment){
        db.collection('CR_user').where({
          telphone : this.data.comment[i].userTelphone
        }).get({
          success:(e)=>{
            user[i].headImage = e.data[0].headImage
            user[i].username = e.data[0].username
          }
        })
      }
      setTimeout(()=>{
        that.setData({
          comment : user
        })
      },1000)
      },
    yunGetByName(data){
        //云数据根据食物名称查询查询
        let db = wx.cloud.database()
        let that = this
        db.collection('CR_foods')
        .where({foodName:data})
        .get({
          success:(res)=>{
            that.setData({
                foodDetails:res.data
            })
          }
        })
      },
      numRemove(){
          let number = this.data.num
          if (number>0) {
              this.setData({
                  num:number-1
                })
            }
        },
        numAdd(){
          let number = this.data.num
          if (number>=0) {
              this.setData({
                  num:parseInt(number)+1
              })
          }
      },
      payHandler(){
          let that = this
          let foodDetail = this.data.foodDetails[0]
          if (this.data.num>0) {
              wx.navigateTo({
                url: '/pages/stores/stores?storeName='+foodDetail.store+'&&num='+that.data.num+'&&_id='+foodDetail._id
              })
              //云数据添加订单数据
            //   let db = wx.cloud.database()
            //   let that = this
            //   let num = this.data.num
            //   let foodName = this.data.foodName
            //   db.collection('CR_orders')
            //   .add({
            //       data:{num:num,
            //       foodName:foodName,
            //       total:that.data.foodDetails[0].price*num},
            //       success:(res)=>{
            //           console.log(res);
            //       }
            //   })
            //   wx.showToast({
            //     title: '加入订购成功',
            //     duration: 1500,
            //     icon:'success'
            //   })
          }else{
              wx.showToast({
                title: '请选择订购数量',
                duration: 1500,
                icon:'error'
              })
          }
      },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.num) {
            this.setData({
                foodName:options.foodName,
                num:options.num
            })
        }
        this.setData({
            foodName:options.foodName
        })
        this.yunGetByName(this.data.foodName)
        setTimeout(()=>{
          // 获取评价列表
           this.getCommentList()
         },500)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})